package com.avic.rong.goodsSort.adapter.secondListAdapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.product.DetailProductActivity;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchAllBzTimeBean;
import com.avic.rong.utils.StringUtils;
import com.avic.rong.wedgit.StringUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Chenjiaxin on 2019/7/20 12:55
 * E-Mail Address：16601203965@163.com
 */
public class GoodsSortBzShowTimeRvAdapter extends RecyclerView.Adapter<GoodsSortBzShowTimeRvAdapter.GoodsSortBzShowRvViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<GoodsSortSearchAllBzTimeBean.DataBean> timeBeanList = new ArrayList<>();
    private GoodsBzTimeRvListener goodsBzTimeRvListener;

    public GoodsBzTimeRvListener getGoodsBzTimeRvListener() {
        return goodsBzTimeRvListener;
    }

    public void setGoodsBzTimeRvListener(GoodsBzTimeRvListener goodsBzTimeRvListener) {
        this.goodsBzTimeRvListener = goodsBzTimeRvListener;
    }

    public GoodsSortBzShowTimeRvAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void setTimeBeanList(List<GoodsSortSearchAllBzTimeBean.DataBean> timeBeanList) {
        this.timeBeanList = timeBeanList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public GoodsSortBzShowRvViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.search_result_peixun_item, viewGroup, false);
        return new GoodsSortBzShowRvViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GoodsSortBzShowRvViewHolder holder, final int i) {

        String title = timeBeanList.get(i).getTitle();
        String title1 = title.substring(timeBeanList.get(i).getNo().length());
        if (!StringUtil.isEmpty(title1)) {
            holder.title.setText(title1);
        } else {
            holder.title.setText(timeBeanList.get(i).getTitle());
        }

        //设置标题筛选高亮
//        if (timeBeanList != null){
//            SpannableString spannableString = new SpannableString(timeBeanList.get(i).getNo());
//            int startIndex = timeBeanList.get(i).getNo().toLowerCase().indexOf(SearchHomePageActivity.nowSearchStr.toLowerCase());
//            if (startIndex>=0){
//                ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_f23030));
//                spannableString.setSpan(colorSpan, startIndex, startIndex+SearchHomePageActivity.nowSearchStr.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
//                holder.no.setText(spannableString);
//            }else {
//                holder.no.setText(timeBeanList.get(i).getNo());
//            }
//        }
        DecimalFormat df = new DecimalFormat("###0.00");
        String format = df.format(timeBeanList.get(i).getPrice());

        String time = timeBeanList.get(i).getPostTime();
        String newTiem = time.substring(0, time.indexOf("T"));
        if (StringUtils.isEmpty( timeBeanList.get(i).getAttrShow())) {
            holder.type.setVisibility(View.GONE);
        }else{
            holder.type.setText(timeBeanList.get(i).getAttrShow());
        }
        holder.no.setText(timeBeanList.get(i).getNo());
        holder.title.setText(timeBeanList.get(i).getTitle());
        holder.fabudata.setText("发布日期:" + newTiem);
        holder.shishidata.setText("实施日期:" + newTiem);
        holder.price.setText("¥" + format);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = timeBeanList.get(i).getUrl();
                String[] split = s.split("/");
                Log.e("cjx", "URL：" + split[2]);
                context.startActivity(DetailProductActivity.getIntent(context, split[2]));
            }
        });
        holder.car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = timeBeanList.get(i).getUrl();
                String[] split = s.split("/");
                String skuStr = "{" + "\"" + split[2] + "\"" + ":" + "1}";
                Log.e("cjx", "splite:" + split[2]);
                goodsBzTimeRvListener.addBzTimeCar(skuStr,holder);
            }
        });

    }

    @Override
    public int getItemCount() {
        return timeBeanList.size();
    }

    public class GoodsSortBzShowRvViewHolder extends RecyclerView.ViewHolder {
        TextView type, no, price, title, fabudata, shishidata;
        public ImageView car;

        public GoodsSortBzShowRvViewHolder(@NonNull View itemView) {
            super(itemView);
            type = itemView.findViewById(R.id.view_goods_Bz_TypeName);
            no = itemView.findViewById(R.id.view_goods_Bz_No);
            price = itemView.findViewById(R.id.view_goods_Bz_Price);
            title = itemView.findViewById(R.id.view_goods_Bz_Desc);
            fabudata = itemView.findViewById(R.id.view_goods_Bz_FaBuData);
            shishidata = itemView.findViewById(R.id.view_goods_Bz_ShiShiData);
            car = itemView.findViewById(R.id.view_goods_Bz_Car);
        }
    }

    public interface GoodsBzTimeRvListener {
        void addBzTimeCar(String skuStr,GoodsSortBzShowRvViewHolder holder);
    }
}
